#!/usr/bin/env php
<?php

class Colors {
	private $foreground_colors = array();
	private $background_colors = array();

	public function __construct() {
		// Set up shell colors
		$this->foreground_colors['black'] = '0;30';
		$this->foreground_colors['dark_gray'] = '1;30';
		$this->foreground_colors['blue'] = '0;34';
		$this->foreground_colors['light_blue'] = '1;34';
		$this->foreground_colors['green'] = '0;32';
		$this->foreground_colors['light_green'] = '1;32';
		$this->foreground_colors['cyan'] = '0;36';
		$this->foreground_colors['light_cyan'] = '1;36';
		$this->foreground_colors['red'] = '0;31';
		$this->foreground_colors['light_red'] = '1;31';
		$this->foreground_colors['purple'] = '0;35';
		$this->foreground_colors['light_purple'] = '1;35';
		$this->foreground_colors['brown'] = '0;33';
		$this->foreground_colors['yellow'] = '1;33';
		$this->foreground_colors['light_gray'] = '0;37';
		$this->foreground_colors['white'] = '1;37';

		$this->background_colors['black'] = '40';
		$this->background_colors['red'] = '41';
		$this->background_colors['green'] = '42';
		$this->background_colors['yellow'] = '43';
		$this->background_colors['blue'] = '44';
		$this->background_colors['magenta'] = '45';
		$this->background_colors['cyan'] = '46';
		$this->background_colors['light_gray'] = '47';
	}

	// Returns colored string
	public function getColoredString($string, $foreground_color = null, $background_color = null) {
		$colored_string = "";

		// Check if given foreground color found
		if (isset($this->foreground_colors[$foreground_color])) {
			$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
		}
		// Check if given background color found
		if (isset($this->background_colors[$background_color])) {
			$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
		}

		// Add string and end coloring
		$colored_string .=  $string . "\033[0m";

		return $colored_string;
	}

	// Returns all foreground color names
	public function getForegroundColors() {
		return array_keys($this->foreground_colors);
	}

	// Returns all background color names
	public function getBackgroundColors() {
		return array_keys($this->background_colors);
	}
}

chdir(dirname(__FILE__));
system("clear");

$colors = new Colors;
echo $colors->getColoredString("     FRAMECALIXTO - www.framecalixto.com     ", "white","yellow") . "\n\n\n";
echo $colors->getColoredString(" Opções: ", "yellow") . "\n";
echo $colors->getColoredString("     iniciar      Define permissões nos diretórios do projeto", "yellow") . "\n";
echo $colors->getColoredString("     lc           Limpa o cache dos arquivos do renderizador Smarty", "yellow") . "\n\n\n";
echo $colors->getColoredString(" Diretório: ".getcwd(), "brown") . "\n\n\n";

if(isset($argv[1])){
    switch($argv[1]){
        case "lc":
            echo $colors->getColoredString(" Limpando cache...", "yellow") . "\n\n\n";
            $files = scandir(getcwd()."/.tmp/");
            $totalExcluidos = 0;
            $totalFalhas = 0;
            for($i=0;$i<count($files);$i++){
                if(!in_array($files[$i],array('.','..','.null','.svn'))){
                    echo " Excluir: ".$colors->getColoredString($files[$i], "white")." ";
                    if(unlink(getcwd()."/.tmp/".$files[$i])) { echo $colors->getColoredString("OK", "green")."\n"; $totalExcluidos++; }
                    else { echo $colors->getColoredString("FALHOU", "green")."\n"; $totalFalhas++; }
                }
            }
            
            if($totalExcluidos > 0 ) { echo "\n\n".$colors->getColoredString(" Total de arquivos excluídos: {$totalExcluidos}", "green")."\n"; }
            if($totalFalhas > 0 ) { echo "\n\n".$colors->getColoredString(" Total de falhas: {$totalFalhas}", "red")."\n"; }
            
            break;
        case "iniciar":
            echo $colors->getColoredString(" Definindo permissões nos diretórios do projeto...", "yellow")." ";
            if(chmod(getcwd()."/.sistema/xml/definicoes.xml",0777)){ echo $colors->getColoredString("OK", "green")."\n"; }
            if(chmod(getcwd()."/.tmp",0777)){ echo $colors->getColoredString("OK", "green")."\n"; }
            else { echo $colors->getColoredString("FALHOU", "green")."\n"; }
            break;
    }
}

echo "\n";

?>
